﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics.Eventing.Reader;
using System.Net.NetworkInformation;



namespace ConsoleApp2.组件
{
    public class Cylinder
    {
        //动作安全配置
        public bool homeSafe = false;
        public bool workSafe = false;
        //IO输入配置
        public bool InputHome = false;
        public bool InputWork = false;
        //IO输出配置
        public bool OutputHome = false;
        public bool OutputWork = false;
        //参数设置
        public bool bHomeShield;//原位屏蔽设置
        public bool bWorkShield;//原位屏蔽设置
        public int uHomeFilterTime = 500;//原位滤波时间
        public int uWorkFilterTime = 500;//动位滤波时间
        public int uShieldTime = 2000;//屏蔽用时
        public int uTimeOut = 3500;//动作超时：ms

        //气缸报警
        public bool bCylinderAlm = false;//气缸报警
        bool bHomeDelayAlm = false;//原位延时报警
        bool bWorkDelayAlm = false;//动位延时报警
        bool bNotSafeAlm = false;//不安全报警
        bool bNotAtPosAlm = false;//不在位报警

        //输出操作日志
        //public string CylsLog = "时间:" + DateTime.Now.ToString();

        //执行原点动作
        public bool ExtHome()
        {
            bool bExtHomeDone = false;
            string str = "开始执行原点动作";
            Console.WriteLine(str);
            if (HomeJudgeSafe(homeSafe))
            {
                //执行原位io输出
                OutputHome = true;
                OutputWork = false;
                string str1 = "原位io输出";
                bExtHomeDone = true;//执行成功
                Console.WriteLine(str1);
            }
            else
            {
                bCylinderAlm= true;
                bNotSafeAlm = true;
                bExtHomeDone = false;//执行失败

            }
            return bExtHomeDone;
        }
        //执行动点动作
        public bool ExtWork()
        {
            bool bExtWorkDone = false;
            string str = "开始执行动点动作";
            Console.WriteLine(str);
            //CylsLog = CylsLog + str;
            if (WorkJudgeSafe(workSafe))
            {
                //执行动位io输出
                OutputHome = false;
                OutputWork = true;
                string str1 = "动点io输出";
                Console.WriteLine(str1);
                bExtWorkDone = true;//执行成功
            }
            else
            {
                bCylinderAlm = true;
                bNotSafeAlm = true;
                bExtWorkDone = false;
            }
            return bExtWorkDone;
        }
        //原位安全状态判定
        bool HomeJudgeSafe(bool bHomeSafe)
        { //默认安全
            bool HomeSafe = false;
            string str = null;
            if (!bHomeSafe)
            {
                HomeSafe = true;
                str = "原位动作判定安全，可执行原位动作";
            }
            else
            {
                str = "原位动作判定不安全，禁止原位动作！！！";
            }
            Console.WriteLine(str);
            return HomeSafe;
        }
        //原位状态获取
        public bool GetHomeStatus()
        {
            
            if (bHomeShield)
            {
                Thread.Sleep(uShieldTime);
                string str = "原位到达";
                Console.WriteLine(str);
                return true;
            }
            else
            {
                //等待输入信号到达后延时相应时间
                int count = 0;
                while (count * 10 < uTimeOut)
                {
                    if (InputHome)
                    {
                        Thread.Sleep(uHomeFilterTime);
                         string str1 = "原位到达";
                        Console.WriteLine(str1);
                        return true;
                    }
                    Thread.Sleep(10);//0.01ms判定一次
                    count++;
                }
                //超时，输出报警
                bCylinderAlm = true;
                bHomeDelayAlm = true;
                string str = "原位动作执行超时报警！";
                Console.WriteLine(str);
                return false;
            }
        }

        //动点安全状态判定
        bool WorkJudgeSafe(bool bWorkSafe)
        {
            //默认安全
            bool WorkSafe = false;
            string str = null;
            if (!bWorkSafe)
            {
                WorkSafe = true;
                str = "动位动作判定安全，可执行动位动作";
            }
            else
            {
                str = "动位动作判定不安全，禁止动位动作！！！";
            }
            Console.WriteLine(str);
            return WorkSafe;
        }
        //动点状态获取
        public bool GetWorkStatus()
        {
            if (bWorkShield)
            {
                Thread.Sleep(uShieldTime);
                string str = "动位到达";
                Console.WriteLine(str);
                return true;
            }
            else
            {
                //等待输入信号到达后延时相应时间
                int count = 0;
                while (count * 10 < uTimeOut)
                {
                    if (InputWork)
                    {
                        Thread.Sleep(uWorkFilterTime);
                        string str1 = "动位到达";
                        Console.WriteLine(str1);
                        return true;
                    }
                    Thread.Sleep(10);//0.01ms判定一次
                    count++;
                }
                //超时，输出报警
                bCylinderAlm = true;
                bWorkDelayAlm = true;
                string str = "动位动作执行超时报警！";
                Console.WriteLine(str);
                return false;
            }
        }
        // 清除报警
        public void ClearAlarm()
        {
            string str = "开始清除报警";
            Console.WriteLine(str);
            bCylinderAlm = false;//气缸报警
            bHomeDelayAlm = false;//原位延时报警
            bWorkDelayAlm = false;//动位延时报警
            bNotSafeAlm = false;//不安全报警
            bNotAtPosAlm = false;//不在位报警
        }
        //输出日志函数    
        public void WriteLog(string str)
        {
            Console.WriteLine(str);
        }
    }
}

